package com.rppowell.pixie.swing.importer;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class SpriteSetImporterImageCanvas extends JPanel {
	private static final long serialVersionUID = 1L;
	private BufferedImage image;
	
	public BufferedImage getImage() {
		return image;
	}
	
	public void setImage(BufferedImage image) {
		if (image != null) {
			this.image = image;
			setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
		} else{
			setPreferredSize(new Dimension(0, 0));
			this.image = null;
		}
		// update to calculate component layout (invalidate() does not update scrollpane)
		this.revalidate();
		this.repaint();
	}
	
	public void paint(Graphics g) {
		Insets i = getInsets();
		g.setColor(getBackground());;
		g.fillRect(i.left,  i.top,  getWidth()-i.left-i.right, getHeight()-i.top-i.bottom);;
		if (image != null) {
			g.drawImage(image, 0, 0, null);
		}
	}

}
